package dbsync4j.mapper.concrete.compare;

import java.util.Collection;
import java.util.Map;

import dbsync4j.core.behavior.DatabaseObject;
import dbsync4j.mapper.behavior.compare.DifferenceCompare;
import dbsync4j.mapper.behavior.diff.Difference;
import dbsync4j.mapper.concrete.diff.AttributeValueDifference;
import dbsync4j.mapper.concrete.diff.NullPointerDifference;

public class ConcreteColumnDifferenceCompare extends AbstractDifferenceCompare
implements DifferenceCompare {

	public ConcreteColumnDifferenceCompare(DatabaseObject sourceColumn, DatabaseObject destinyColumn) {
		
		super(sourceColumn, destinyColumn);

	}

	/* (non-Javadoc)
	 * @see dbsync4j.mapper.behavior.compare.DifferenceCompare#diff()
	 */
	@Override
	public Collection <Difference> diff() {
		
		String tag = null;
		
		/* verifica se a lista de diferen�as j� possui ao menos um item */
		if(differences.size() == 0 ) { 
			if(this.source == null && this.destiny == null) {

				return differences;
			
			} else if(this.source == null || this.destiny == null) {

				tag = "columnpair";
				differences.add(new NullPointerDifference(source, destiny, tag));

			} else {
				
				tag = "columns";
				Map<String, String> diffResult = compareObjects(tag, source, destiny);
				Collection<String> diffKeys = diffResult.keySet();
				
				for(String d : diffKeys) {
					String attribute = d.replace(tag.concat("."), "");
					differences.add(new AttributeValueDifference(this.source, this.destiny, attribute, tag));
				}
			} 
			
			for(DifferenceCompare d : comparators) {
				differences.addAll(d.diff());
			}
		}

		return differences;
	}

}
